﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Caliburn.PresentationFramework.Filters;
using System.Threading;
using XWord.Globalization;
using Caliburn.Core.Metadata;
using Caliburn.PresentationFramework.ApplicationModel;
using XWord.WPF.ApplicationFramework;
using XWord.Common.Network;
using System.Windows;
using System.Speech.Synthesis;
using System.Collections;
using Caliburn.PresentationFramework.ApplicationModel;

namespace XWord.GUI.Presenters.Infrastructure
{
    [PerRequest(typeof(OptionManagerPresenter))]
    public class OptionManagerPresenter : Presenter
    {
        public OptionManagerPresenter()
        {
            InstalledVoices = synthesizer.GetInstalledVoices();
        }

        protected override void OnInitialize()
        {
            
            base.OnInitialize();
        }


        public void SaveOptions()
        {
            Config.Save();
            this.Close();
        }

        public void ApplyOptions()
        {
            Application.Current.SetAutoRun(Config.AutoRun);
            Config.Save();
        }

        public void ResetDefaultOption()
        {
            Config.Reset();
        }

        public void CancelOption()
        {
            Config.Cancel();
        }

        public XWordAppConfig Config
        {
            get
            {
                return XWordAppConfig.Current;
            }
        }

        public bool CanTestProxy()
        {
            if (Config.Proxy.ProxyType == ProxyType.None || string.IsNullOrEmpty(Config.Proxy.IP) || string.IsNullOrEmpty(Config.Proxy.Port))
            {
                return false;
            }
            return true;
        }

        [Preview("CanTestProxy")]
        public void TestProxy()
        {

            //WebProxyProtocal protocal = new WebProxyProtocal();
            //if (Config.Proxy.ProxyType == ProxyType.Http)
            //    protocal.SetProxy(Config.Proxy);
            //protocal.ErrorResponse += delegate { MessageBox.Show(LanguageReader.GetValue("Infrastructure_OptionManager_ConnectProxyError")); };
            //protocal.AfterResponse += delegate { MessageBox.Show(LanguageReader.GetValue("Infrastructure_OptionManager_ConnectProxySuccess")); };
            //ThreadPool.QueueUserWorkItem(new WaitCallback((object obj) =>
            //{
            //    protocal.Transport(UpdateManager.UpdateURLData.ServerVersionURL);
            //}));

        }

        private readonly SpeechSynthesizer synthesizer = new SpeechSynthesizer();

        public void TestSpeak(string textToSpeak)
        {
            synthesizer.SelectVoice(Config.TTSConfig.VoiceName);
            synthesizer.Rate = Config.TTSConfig.Rate;
            synthesizer.Volume = Config.TTSConfig.Volume;
            synthesizer.Speak(textToSpeak);
        }

        private IList<InstalledVoice> _installedVoices;

        public IList<InstalledVoice> InstalledVoices
        {
            get
            {
                return _installedVoices;
            }
            set
            {
                _installedVoices = value;
                NotifyOfPropertyChange("InstalledVoices");
            }
        }
    }
}
